﻿Public Class perUsuariosSetores
    Inherits perPadrao

#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
#End Region

#Region "Métodos"

    Public Function inserirUsuariosSetores(ByVal iIdUsuario As Int32, _
                                           ByVal iIdSetor As Int32) As Int32

        Dim sSql As String

        Try
            sSql = "  INSERT INTO DC_UsuariosSetores "
            sSql &= " ( "
            sSql &= "   IDUsuario, "
            sSql &= "   IDSetor  "
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= "   ( @IDUsuario, @IDSetor ) "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDUsuario", iIdUsuario)
                .AddWithValue("@IDSetor", iIdSetor)
            End With

            MyBase.executarAcao(sSql)

            Return IIdSetor
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirUsuariosSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarUsuariosSetores(ByVal iIdSetor As Int32, _
                                      ByVal iIdUsuario As String)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_UsuariosSetores SET "
            sSql &= "   IdSetor = @IdSetor "
            sSql &= " WHERE "
            sSql &= "   IdUsuario = @IdUsuario "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IdSetor", iIdSetor)
                .AddWithValue("@IdUsuario", iIdUsuario)
            End With

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarUsuariosSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirUsuariosSetores(ByVal iIdSetor As Int32, _
                                      ByVal iIdUsuario As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_Setores "
            sSql &= " WHERE "
            sSql &= "     IDSetor = @IDSetor "
            sSql &= " AND IDUsuario = @IDUsuario "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IdSetor", iIdSetor)
                .AddWithValue("@IDUsuario", iIdUsuario)
            End With

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirUsuariosSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarUsuariosSetores(ByVal iIDSetor As Int32, _
                                              ByVal iIDUsuario As Int32) As DataTable

        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT "
            sSql &= "   DC_UsuariosSetores.* "
            sSql &= "FROM "
            sSql &= "   DC_UsuariosSetores "

            If iIDSetor > 0 OrElse iIDUsuario > 0 Then
                sSql &= "WHERE "
            End If

            If iIDSetor > 0 Then
                sSql &= "IDSetor = @IDSetor AND "
            End If

            If iIDUsuario > 0 Then
                sSql &= "IDUsuario = @IDUsuario "
            End If

            With MyBase.SQLCmd.Parameters
                .Clear()
                If iIDSetor > 0 Then
                    .AddWithValue("@IDSetor", iIDSetor)
                End If
                If iIDUsuario > 0 Then
                    .AddWithValue("@IDUsuario", iIDUsuario)
                End If
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_UsuariosSetores")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_UsuariosSetores").Rows.Count > 0 Then
                Return dsDados.Tables("DC_UsuariosSetores")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarUsuariosSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region
End Class
